/**
 * @description delete video menu
 * @author wangfupeng
 */

import { IButtonMenu, IDomEditor, DomEditor, SlateTransforms } from '@wangeditor/editor';

class DeleteVideoMenu implements IButtonMenu {
	readonly title = '删除';
	readonly iconSvg =
		'<svg t="1723430430767" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2311" width="200" height="200"><path d="M202.666667 256h-42.666667a32 32 0 0 1 0-64h704a32 32 0 0 1 0 64H266.666667v565.333333a53.333333 53.333333 0 0 0 53.333333 53.333334h384a53.333333 53.333333 0 0 0 53.333333-53.333334V352a32 32 0 0 1 64 0v469.333333c0 64.8-52.533333 117.333333-117.333333 117.333334H320c-64.8 0-117.333333-52.533333-117.333333-117.333334V256z m224-106.666667a32 32 0 0 1 0-64h170.666666a32 32 0 0 1 0 64H426.666667z m-32 288a32 32 0 0 1 64 0v256a32 32 0 0 1-64 0V437.333333z m170.666666 0a32 32 0 0 1 64 0v256a32 32 0 0 1-64 0V437.333333z" fill="#000000" p-id="2312"></path></svg>';
	readonly tag = 'button';

	getValue(editor: IDomEditor): string | boolean {
		// 无需获取 val
		return '';
	}

	isActive(editor: IDomEditor): boolean {
		// 无需 active
		return false;
	}

	isDisabled(editor: IDomEditor): boolean {
		if (editor.selection == null) return true;

		const videoNode = DomEditor.getSelectedNodeByType(editor, 'davinci-media');
		if (videoNode == null) {
			// 选区未处于 video node ，则禁用
			return true;
		}
		return false;
	}

	exec(editor: IDomEditor, value: string | boolean) {
		if (this.isDisabled(editor)) return;
		// 删除视频
		SlateTransforms.removeNodes(editor, {
			match: n => DomEditor.checkNodeType(n, 'davinci-media')
		});
	}
}

export default DeleteVideoMenu;
